#!/bin/bash
#
#   This file is part of poat.
#
#   poat is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   poat is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with poat.  If not, see <http://www.gnu.org/licenses/>.
#

export LANG="en_US"
export TMP="/tmp"
export PODIR="$TMP/pofiles"
export PRLIST="projectlist"
export FLIST="filelist"
export POAT="http://localhost/poat/g.php?w"

#string exec  ( string $comando  [, array &$salida  [, int &$var_retorno  ]] )

function cleaning {
	if [ -d $PODIR ]
	then
		echo "$TMP already exists"
		rm -rf $PODIR
		mkdir $PODIR
	else
		echo "Creating directory $TMP"
		mkdir $PODIR
	fi
}

#OBTENER LISTA DE PROJECTOS
echo "Get Project List"
wget -O $TMP/$PRLIST $POAT'=prj'

for PJ in $(cat $TMP/$PRLIST)
do
#OBTENER LISTA DE ARCHIVOS
	cleaning
	echo "Get File List For $PJ"
	wget -O $TMP/$FLIST $POAT'=file&pid='$PJ
	cd $PODIR
	for PFI in $(cat $TMP/$FLIST)
	do
		POFILE=`echo $PFI | cut -f2 -d';'`
		URL=`echo $PFI | cut -f3 -d';'`
		FID=`echo $PFI | cut -f1 -d';'`
		echo "Get File $POFILE"
		wget --no-check-certificate $URL	

		if `msgfmt --check-header $POFILE >& /dev/null`
		then
			msgfmt --statistics $POFILE >& $TMP/filestats.txt
			POTRANS=`cat  $TMP/filestats.txt| gawk -F, '{print $1}' | sed 's/ translated messages//'` 
		
			if `echo $POTRANS | grep \. >/dev/null`
			then
				POTRANS=`echo $POTRANS| sed 's/\.//'`
			fi
			if `echo $POTRANS | grep "translated message" >/dev/null`
			then
					POTRANS=`echo $POTRANS| sed 's/ translated message//'`
			fi
			if [ "$POTRANS" == "" ]
			then
				POTRANS="0"
			fi
		
			#in stats are fuzzy skipped, if they are 0 
			if `cat $TMP/filestats.txt | grep fuzzy >/dev/null`
			then
				POFUZZY=`cat  $TMP/filestats.txt|  gawk -F, '{print $2}' | sed 's/ fuzzy translations//' | sed 's/ //'`
				if `echo $POFUZZY | grep "fuzzy translation" >/dev/null`
				then
					POFUZZY=`echo $POFUZZY| sed 's/ fuzzy translation//'`
				fi
				if `echo $POFUZZY | grep \. >/dev/null`
				then
					POFUZZY=`echo $POFUZZY| sed 's/\.//'`
				fi
				if [ "$POFUZZY" == "" ]
				then
						POFUZZY="0"
				fi
				
				POUN=`cat  $TMP/filestats.txt|  gawk -F, '{print $3}' | sed 's/ untranslated messages\.//' | sed 's/ //'`
				if `echo $POUN | grep "untranslated message" >/dev/null`
				then
					POUN=`echo $POUN| sed 's/ untranslated message.//'`
				fi
				if [ "$POUN" == "" ]
				then
					POUN="0"
				fi
			else
					POFUZZY="0"
					POUN=`cat  $TMP/filestats.txt|  gawk -F, '{print $2}' | sed 's/ untranslated messages\.//' | sed 's/ //'`
					if `echo $POUN | grep "untranslated message" >/dev/null`
					then
						POUN=`echo $POUN| sed 's/ untranslated message.//'`
					fi
					if [ "$POUN" == "" ]
					then
						POUN="0"
					fi
			fi
			echo "Save File $POFILE"
			wget -O $TMP/res $POAT'=put&pid='$PJ'&fid='$FID'&t='$POTRANS'&f='$POFUZZY'&u='$POUN
			cat $TMP/res
		fi	
		
	done
done
cleaning
echo "Done"
